﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Quick.Common.CacheStorage;
using Quick.Common.Components;
using Quick.Common.Logging;

namespace PortalWeb.Common.UI
{
    public static class ActionHelper
    {
        public static ICacheStorage CacheStorage = ObjectContainer.Resolve<ICacheStorage>();

        public static async Task<List<ControllerInfo>> GetControllersAsync(Assembly assembly)
        {
            return await Task.Run(() =>
            {
                List<ControllerInfo> controllers;
                if (CacheStorage.Exist("AllController"))
                {
                    controllers = CacheStorage.Get("AllController") as List<ControllerInfo>;
                    if (controllers != null)
                        return controllers;
                }
                controllers = new List<ControllerInfo>();
                var types = assembly.GetTypes();
                CacheStorage.Insert("AllController", types);

                foreach (var type in types)
                {
                    if (type.BaseType == null)
                        continue;
                    if (type.BaseType.Name == "Controller") //如果是Controller
                    {
                        //设置Controller数组
                        object[] attrs =
                            type.GetCustomAttributes(typeof (NavbarAttribute),
                                true);
                        if (attrs.Length <= 0)
                            continue;
                        var navbarAttribute = attrs[0] as NavbarAttribute;
                        if (navbarAttribute == null)
                            continue;
                        string defaultPage = "Index"; //默认首页的控制器
                        var controller = new ControllerInfo
                        {

                            Code = type.Name.Replace("Controller", ""),
                            ParentName = navbarAttribute.ParentName,
                            Name = navbarAttribute.NavbarName
                        };
                        controller.ActionUrl = "/Admin/" + controller.Code + "/" + defaultPage;

                        GetActions(type, out defaultPage);
                        controllers.Add(controller);
                    }
                }
                return controllers;
            });
        }
        /// <summary>
        /// 获取所有的动作(根据控制器的Type)
        /// </summary>
        /// <param name="type">类别</param>
        /// <param name="defaultPage">默认的控制器首页(Action)</param>
        /// <returns></returns>
        public static IList<ActionInfo> GetActions(Type type, out string defaultPage)
        {
            //默认是Index页面
            defaultPage = "Index";
            var members = type.GetMethods();
            var result = new List<ActionInfo>();
            foreach (var member in members)
            {
                if (member.ReturnType.Name == "ActionResult") //如果是Action
                {
                    var item = new ActionInfo
                    {
                        Name = member.Name,
                        ControllerName = member.DeclaringType.Name.Substring(0, member.DeclaringType.Name.Length - 10)
                    };
                    // 去掉“Controller”后缀

                    object[] attrs = member.GetCustomAttributes(typeof (System.ComponentModel.DescriptionAttribute),
                        true);
                    if (attrs.Length > 0)
                    {
                        item.Description = (attrs[0] as System.ComponentModel.DescriptionAttribute).Description;
                    }
                    item.LinkUrl = "/Admin/" + item.ControllerName + "/" + item.Name;
                    result.Add(item);
                    object[] Defaultpages = member.GetCustomAttributes(typeof (DefaultPageAttribute), true);
                    if (Defaultpages.Length > 0)
                        defaultPage = item.Name;
                }
            }
            return result;
        }
    }
}
